package com.lkm.market2_0;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;

import com.lkm.comlib.FXBM;

public class Notice {

	private static int oldNum = 0;

	public static int getId() {
		return Notice.class.hashCode() - 1;
	}

	public static Notification ShowImNotification(Context context,String content, String ticker,boolean isSound) {
		String appName = context.getPackageManager().getApplicationLabel(context.getApplicationInfo()).toString();
		return ShowImNotification(context, appName, content, ticker, isSound,getId());
	}
	public static Notification ShowImNotification(Context context,String title, String content, String ticker,boolean isSound,int id) {
		return ShowNotification(context, title, content, ticker, isSound, id, new Intent(MainActivity.class.getName()));
	}
	public static Notification ShowNotification(Context context,String title, String content, String ticker,boolean isSound,int id,Intent notifyIntent) {
		if (context == null)
			return null;

		// UserInfoCache
		// userInfoCache=MyApplication.getInstance(context).getUserInfoCache();

		

		// notifyIntent.putExtra("toHeadUrl", toHeadUrl);

		PendingIntent appIntent = PendingIntent.getBroadcast(context, id, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);

		NotificationCompat.Builder mNotificationCompat =

		new NotificationCompat.Builder(context).setContentIntent(appIntent).setContentText(content).setContentTitle(title).setTicker(ticker) // 通知首次出现在通知栏，带上升动画效果的
				// .setContentInfo(title)
				.setSmallIcon(R.drawable.ic_logo).setLights(0xff00ff00, 300, 750).setDefaults(android.app.Notification.DEFAULT_LIGHTS)
				// .setOngoing(true)
				.setAutoCancel(true).setWhen(System.currentTimeMillis())// 通知产生的时间，会在通知信息里显示，一般是系统获取到的时间
				.setNumber(oldNum);//oldNum先将就着吧
		if(isSound)
			mNotificationCompat.setDefaults(Notification.DEFAULT_SOUND);
		Notification notify = mNotificationCompat.build();
		((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE)).notify(id, notify);

		return notify;
	}

	public static Notification ShowImNotification(Context context, int unReadUserNum) {
		if(unReadUserNum<1){
			return ShowImNotificationApp(context);
		}
		
		String appName = context.getPackageManager().getApplicationLabel(context.getApplicationInfo()).toString();
		String content = "有" + (unReadUserNum) + "个待处理订单，请尽快处理";// 过来"+userInfoCache.UnReadMsgNum+"条新消息";
		String ticker = appName + "：您有新的订单";// uName+"："+msg;
		boolean isSound=unReadUserNum!=oldNum;//有变化
		oldNum=unReadUserNum;
		
		FXBM.D();//应该是使用最新的订单号还判断是否有新的单吧
		if(!isSound){return null;}//单数没变化就不提示
		
		return ShowImNotification(context, content, ticker,isSound);
	}

	public static Notification ShowImNotificationApp(Context context) {
//		oldNum=0;
		System.out.println("==========ShowImNotificationApp");
		String appName = context.getPackageManager().getApplicationLabel(context.getApplicationInfo()).toString();
		return ShowImNotification(context, appName + "：为您服务", appName,false);
	}

	public static void disImNotification(Context context) {
		oldNum=-1;
		((NotificationManager) context. getSystemService(Context.NOTIFICATION_SERVICE)).cancelAll();
	}

	/*
	 * private static final String base= Notice.class.getName(); public static
	 * final String Action_TucaoLs_Update=base+"Action_TucaoLs_Update";
	 * 
	 * 
	 * public static void sendBroadcast(Context c,String action){
	 * LocalBroadcastManager.getInstance(c).sendBroadcast(new Intent(action)); }
	 * public static void registerReceiver(Context c,BroadcastReceiver br,
	 * String action){ LocalBroadcastManager.getInstance(c).registerReceiver(br,
	 * new IntentFilter(action)); }
	 * 
	 * public static void unregisterReceiver(Context c,BroadcastReceiver br){
	 * LocalBroadcastManager.getInstance(c).unregisterReceiver(br); }
	 */
}
